<?php
/*$this->load->view('templates/page_head');
$this->load->view('templates/admin/js_css_includes.php');*/
$this->load->view(CMF_PREFIX . 'includes/page_start');

/* edit here change table to controller.  if this works should replace $table with $controller */
$table = $controller;

$show_select = $show_live || $show_archive || $show_delete;
if($sortable){
	?>
<script type="text/javascript">
	var sortables = []
	Event.observe(window, 'load', function(){
		var s = $('sortable');
		if( s ){
			Sortable.create('sortable', {'tag': 'tr', 'handle': 'handle'}); //, handle: '.handle'
			var f = $('sort_order_form');
			var l = $$('.save_sort_order');
			if( f ){
				l.invoke('observe', 'click', function(e){
					try{
						e.stop();
					}
					catch(ex){}
					f.sort_order.value = Sortable.serialize('sortable');
					f.submit();
				});
			}
		}
	});
</script>
	<?
}
?>
<title>View <?= $table_name ?></title>
</head>
<body>
<?php
//$this->load->view('templates/admin/header');
if($sortable){
	?>
<form action="<?= base_url() . CMF_PREFIX . $table ?>/savesortorder" method="post" id="sort_order_form">
	<input type="hidden" name="sort_order" id="sort_order" />
</form>
	<?
}
?>
<table id="container_table" class="admin_table table_view_container">
	<tr>
		<td>
<?php $this->load->view(CMF_PREFIX . (isset($menu_view)&&!empty($menu_view) ? $menu_view:'includes/menu')); ?>
		</td>
		<td>
<div class="admin_container">
<?php
if( $show_select ){
	?>
<form action="<?= base_url() . CMF_PREFIX . $table ?>/editmultiple/<?= $is_archive?'showarchive':'' ?>" method="post">	
	<?
}
?>
	<h2>View/Edit <?= $table_name ?></h2>
	<?php
	$flash = $this->session->flashdata('cmf_message');
	if( $flash ){
		$this->load->view(CMF_PREFIX . 'includes/flash_view', array('message' => $flash));
	}
	?>
	<a class="add_link" href="<?= base_url() . CMF_PREFIX . $table ?>/edit" title="Add Entry">Add Entry</a>
<table class="edit_table" border="0" cellpadding="0" cellspacing="0">
	<thead>
	<tr>
		<?php
		if( $show_select ){
			?>
			<th>&nbsp;</th>
			<?	
		}
		if( count($view_fields)){
			foreach($view_fields as $key => $value){
				echo '<th>';
				if( isset($value['sort']) && $value['sort'] ){
					$dir = 'ASC';
					$class = "ascending";
					if( $sort_field == $key){
						if( $sort_order == 'ASC'){
							$dir = 'DESC';
							$class = 'descending';
						}
						$class .= ' sort_field';
					}
					echo '<a class="' . $class . '" href="' . base_url() . CMF_PREFIX . $table . '/index/' . $key . '/' . $dir . '/">' . $value['name'] . "</a>\n";
				}else{
					echo $value['name'];
				}
				echo "</th>\n";	
			}
		}
		if( $show_live ){
			?>
			<th>Live</th>
			<?
		}
		?>
		<th>Edit</th>
		<?
		if( $show_archive){
			?>
			<th>Archive</th>
			<?
		}
		if( $show_delete ){
			?>
			<th>Delete</th>
			<?
		}
		?>
	</tr>
	</thead>
	<tbody <?= $sortable?' id="sortable"':'' ?>>
	<?
	$j=0;
	foreach( $items as $item){
		?>
	<tr id="row_<?= $item[$id_field] ?>" class="<?= $j%2?'odd_row':'even_row' ?>">
		<?php
		if( $show_select ){
			?>
			<td><input type="checkbox" name="selected[]" id="select_<?= $item[$id_field] ?>" value="<?= $item[$id_field] ?>" /></td>
			<?	
		}
		/* should move this logic into a library funtion so we don't have to repeat it in every view
		 * or move this into the controller
		 */
		if( count($view_fields)){
			$i=0;
			foreach( $view_fields as $key => $value){
				echo '<td' . ($sortable&&$i==0?' class="handle"':'') . '>';
				if( is_array($value['view']) ){
					$v = $value['view'];
					if( isset($v['func']) && is_callable($v['func'])){
						$p = isset($v['params']) && is_array($v['params'])?$v['params']:array();
						array_push( $p, $item[$key], $table, $key);
						echo call_user_func_array( $v['func'], $p) . '</td>';
					}
				}else if( is_string($value['view'])){
					echo $value['view'];
				}else if( isset($item[$key])){
					echo $item[$key] . '</td>';
				}else{
					echo "&nbsp;";
				}
				$i++;
			}
		}
		if( $show_live ){
			?>
			<td><a href="<?= base_url() . CMF_PREFIX . $table . '/' . ($item['is_live']?'setnotlive':'setlive') . '/' . $item[$id_field] ?>" title="Edit Entry"><?= $item['is_live']?'set not live':'set live' ?></a></td>
			<?
		}
		?>
		<td><a href="<?= base_url() . CMF_PREFIX . $table ?>/edit/<?= $item[$id_field] ?>" title="Edit Entry">edit</a></td>
		<?
		if( $show_archive ){
			if( $is_archive ){
				?>
				<td><a href="<?= base_url() . CMF_PREFIX . $table ?>/restore/<?= $item[$id_field] ?>" title="Archive Entry">restore</a></td>
				<?
			}else{
				?>
				<td><a href="<?= base_url() . CMF_PREFIX . $table ?>/archive/<?= $item[$id_field] ?>" title="Archive Entry">archive</a></td>
				<?
			}
		}
		if( $show_delete ){
			?>
			<td><a href="<?= base_url() . CMF_PREFIX . $table ?>/delete/<?= $item[$id_field] ?>" title="Delete Entry">delete</a></td>
			<?
		}
		?>
	</tr>	
		<?
		$j++;
	}
	?>
	</tbody>
</table>
<div class="actions_container">
<?php
if( $show_select ){
	/* TODO: move this into external javascript file and add to top of file */
	?>
	<script type="text/javascript">
		function processSelectLinks(){
			$$('.select_all').each(function(elm){
				elm.observe('click', function(evt){
					try{
						evt.stop();
					}catch(ex){}
					$$('[id^=select_]').each(function(selm){
						selm.checked = true;
					});
				});
			});
			$$('.deselect_all').each(function(elm){
				elm.observe('click', function(evt){
					try{
						evt.stop();
					}catch(ex){}
					$$('[id^=select_]').each(function(selm){
						selm.checked = false;
					});
				});
			});
		}
		Event.observe(document, 'dom:loaded', processSelectLinks);
	</script>
	<div class="select_boxes">
		<a href="#" class="select_all">Select All</a>
		<a href="#"	class="deselect_all">Deselect All</a>
	</div>
	<?
}
?>
<div class="select_actions">
<?
if( $show_live ){
	?>
	<input type="submit" name="set_live" value="Set Selected Live" />
	<input type="submit" name="set_not_live" value="Set Selected Not Live" />
	<?
}
if( $show_archive ){
	if( $is_archive ){
		?>
		<!-- <a href="/admin/<?= $table ?>/" class="button_link"><button type="button">View List</button></a> -->
		<a href="<?= base_url() . CMF_PREFIX . $table ?>/" class="button_link">View List</a>
		<input type="submit" name="restore" value="Restore Selected" />
		<?
	}else{
		?>
		<!-- <a href="/admin/<?= $table ?>/showarchive" class="button_link"><button type="button">View Archive</button></a> -->
		<a href="<?= base_url() . CMF_PREFIX . $table ?>/showarchive" class="button_link">View Archive</a>
		<input type="submit" name="archive" value="Archive Selected" />
		<?
	}
}
if( $show_delete ){
	?>
	<input type="submit" name="delete"	value="Delete Selected" />
	<?
}
?>
<?
if( $sortable ){
	?>
<!-- <a href="#" title="Save Order" class="save_sort_order button_link"><button type="button">Save Order</button></a> -->
<a href="#" title="Save Order" class="save_sort_order button_link">Save Order</a>
	<?
}
?>
</div>
</div>
<?php
if( $show_select ){
	?>
</form>	
	<?
}
?>
</div>
		</td>
	</tr>
</table>
<?php
//$this->load->view('templates/page_foot');
$this->load->view(CMF_PREFIX . 'includes/page_end');
?>